<?php
/**
 * 
 * @author tangbl
 *
 */
class CommonAction extends Action {

    /**
     * 初始化
     * 子类重写_initialize方法需要调用parent::_initialize();
     */
    public function _initialize() {
		
    	$t     = C('VAR_TEMPLATE');
		$theme = $this->_param("$t","","");
		if(empty($theme)){//待处理
			$theme = C('DEFAULT_THEME');
		}
		if(!strpos('|'.C('THEME_LIST'),$theme)){
			$theme = C('DEFAULT_THEME');
		}
		cookie('think_template',$theme,3600);
		
		$NOW_THEME_PATH = TMPL_PATH.$theme.'/';
		$NOW_THEME_URL  = TMPL_URL .$theme.'/';
		
		$this->assign('NOW_THEME_PATH',$NOW_THEME_PATH);
		$this->assign('NOW_THEME_URL' ,$NOW_THEME_URL);
		define('NOW_THEME_NAME', $theme);
		
		$value = get_syscfig_key("webname".LANG_SET);
		$this->assign('webname' ,$value);
		
		$this->checkMBAC();
		$this->_initmenu();
    }
    
    //检查权限
    public function checkMBAC(){
    	
    	$useron = C("USER_AUTH_ON");
    	
    	if ($useron) {
    		$userid = session(C('USER_AUTH_KEY'));
    		if(empty($userid)){
    			session(C('REFER_URL'),$this->_server("REQUEST_URI"));
    			redirect(PHP_FILE.C('USER_AUTH_GATEWAY'));
    		}else{
    			$this->checkPermission();
    		}
    	}
    }
    
    private function checkPermission($role){
    	$module   = MODULE_NAME;
    	$action   = ACTION_NAME;
    	$perssion = getConfig("perssion");
    	
    	$flag = false ;
    	if(isset($perssion[$module][$action])){
    		$flag = $perssion[$module][$action] ;
    	}
    	if(empty($flag)){
    		$this->error('该用户没有此操作权限');
    	}
    }
    
    //初始化左侧导航
	public function _initmenu(){

    	$nowId    = $this->_param("_menuid","trim","Vipcard");
    	if($nowId){
    		session("_menuid",$nowId);
    	}else{
    		$nowId = session("_menuid");
    	}
    	$roleid = session("hdminRole");
    	
    	if($roleid){
    		$menu = getConfig("menu");
    		foreach($menu as $k => $v){
    			foreach ($v['list'] as $a=>$b){
    				if(!$b['mdmin']){
    					unset($menu[$k]['list'][$a]);
    				}
    				if($a == $nowId){
    					$menu[$k]['list'][$a]['hover'] = "hover";
    				}
    			}
    		}
    		$this->assign("menu",$menu);
    	}
    	
    }
	
	//定义空操作
	public function _empty(){
		$this->error(L('cfg_form_action_noexsits'));
	}
	
    /**
	 * 公共删除方法
	 * @author tangbl
	 * @param  $table 表名
	 * @param  $reply 是否需要更新回复记录
	**/   
	protected function do_del($table){
		$id = I('param.id',0,'');//获取id值
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model   = M("$table");
		$where   = "id in ($id)";
		$res     = $model->where($where)->delete();
		
		if($res) {
			$this->ajaxReturn($res,L('cfg_form_tipmsg_delsucc'),1);
		} else {
			$this->ajaxReturn(0,L('cfg_form_tipmsg_delfail'),0);
		}

    }
	
    /**
	 * 公共添加方法
	 * @author tangbl
	 * @param  $table 表名
	 * @param  $imgae array("single"=>array("single"),"atlas"=>array("atlas"))
	 * @param  $reply 是否需要更新回复记录
	 * @param  $arr   额外的添加参数
	 * @param  $url   自定义跳转路劲數組array("url"=>"Index/index","cateid=1")
	**/ 
    protected function do_add($table,$image=NULL,$arr=NULL,$url=NULL){
        $t = D("$table");
        if($t->create()){
			$solevar = $t->solevar;
			$title   = $t->title;
			if(is_array($image)){
				$single = $image['single'];
				if(is_array($single)){
					foreach($single as $v){
						add_edit_thumb($v, $solevar, $table);//添加缩略图
					}	
				}
				$atlas = $image['atlas'];
				if(is_array($atlas)){
					foreach($atlas as $v){
						add_edit_album($v, $solevar, $table);//添加图集
					}	
				}
			}
			
            if(is_array($arr)){
               foreach($arr as $k=>$v){
                $t->$k=$v;
               }
            }
            $t->addid = session(C('USER_AUTH_KEY'));
            $result   = $t->add();
            if($result){

           		if(is_array($url)){
					$url =U($url['url'],$url['param']);
				}else{
					$url =U($table.'/index');
				}
				$this->assign("jumpUrl",$url);

				$this->success(L('cfg_form_tipmsg_addsucc'));
            }else{
                $this->error(L('cfg_form_tipmsg_addfail'));
            }
        }else{
            $this->error($t->getError());
        }
    }
	
	 /**
	 * 公共修改方法
	 * @author tangbl
	 * @param  $table 表名
	 * @param  $imgae array("single"=>array("single"),"atlas"=>array("atlas"))
	 * @param  $reply 是否需要更新回复记录
	 * @param  $arr   额外的修改参数
	 * @param  $url   自定义跳转路劲數組array("url"=>"Index/index","cateid=1")
	**/ 
    protected function do_edit($table,$image=NULL,$arr=NULL,$url){
		$t = D("$table");
		if($t->create()) {
			$solevar = $t->solevar;
			$title   = $t->title;
			if(is_array($image)){
				$single = $image['single'];
				if(is_array($single)){
					foreach($single as $v){
						add_edit_thumb($v, $solevar, $table);//添加缩略图
					}	
				}$atlas = $image['atlas'];
				if(is_array($atlas)){
					foreach($atlas as $v){
						add_edit_album($v, $solevar, $table);//添加图集
					}	
				}
			}
			
            if(is_array($arr)){
               foreach($arr as $k=>$v){
                $t->$k=$v;
               }
            }
			$result = $t->save();
			if(is_array($url)){
				$url =U($url['url'],$url['param']);
			}else{
				$url =U($table.'/index');
			}
			$this->assign("jumpUrl",$url);
			$this->assign("jumpUrl",U('/'.$table.'/index')); 
			$this->success(L('cfg_form_tipmsg_editsucc'));
		}else{
			$this->error($t->getError());
		}
    }
}